package entity;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Values and methods for specifying the RRULE property
 * for the iCalendar object as defined in RFFC 5545. This
 * property defines a rule or repeating pattern for recurring
 * events.
 * 
 * @author Elliot Ito
 * @version 1.0
 *
 */
public class ExDate implements EntityConstants{
	private String exDate;
	private String exMonth;
	private ArrayList<String> months = new ArrayList<String>();
	
	/**
	 * Constructor for the EXDATE property. All values are defaulted
	 * to null.
	 */
	public ExDate() {
		exDate = null;
		exMonth = null;
	}
	
	
	/**
	 * 
	 * The BY BYMONTHDAY property determines which days of the month 
	 * the recurrence events will take place on. 
	 * @param days An ArrayList containing the days the event will
	 *             occur. [1-31]
	 */
	public void addMonthDayException(ArrayList<String> days) {
		StringBuilder byMonthDayBuilder = new StringBuilder();
		for(int i = 0; i < days.size(); i++) {
			byMonthDayBuilder.append(days.get(i));
			if((days.size() != 1) && (i != days.size() - 1)) byMonthDayBuilder.append(",");
		}
		this.exDate = byMonthDayBuilder.toString();
	}
	
	public void addMonths(ArrayList<String> month)
	{
		for(int x = 0; x < month.size(); x++){
			this.months.add(month.get(x));
		}
	}
	/**
	 * Adds days to the EXDATE with the specified months.
	 * The BY addMonthException property determines which months of the year 
	 * the recurrence events will take place on. 
	 * @param months An ArrayList containing the days the event will not
	 *             occur. [01-12]
	 * @param years An ArrayList containing the years the event will not
	 * 						 occur.
	 */
	public void addMonthException(ArrayList<String> years) {
		StringBuilder byMonthBuilder = new StringBuilder();
		for(int x = 0; x < years.size(); x++){
			byMonthBuilder.append(years.get(x));
			for(int i = 0; i < months.size(); i++) {
				byMonthBuilder.append(months.get(i));
				int lastDay = getDate(Integer.parseInt(months.get(i)), Integer.parseInt(years.get(x)));
				for(int day = 1; day <= lastDay; day++)
				{
					if(day < 10)
					{
						byMonthBuilder.append("0");
					}
					byMonthBuilder.append(day);
					byMonthBuilder.append("T010000Z");
					if((lastDay != 1) && (day != lastDay)) byMonthBuilder.append(",");
				}
			}
		}
		this.exMonth = byMonthBuilder.toString();
	}
	
	
	/**
	 * Returns a string containing the values of the fields of the
	 * EXDATE. Fields that were not added to the EXDATE will not be included. 
	 * @return A String containing a properly formatted EXDATE property.
	 */
	public String toString() {
		StringBuilder rruleString = new StringBuilder("EXDATE:");
		
	    if(exMonth != null) {
	    	rruleString.append(exMonth);
	    }
		rruleString.append("\r\n");
		
		return rruleString.toString();
	}

	private static int getDate(int month, int year){
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, 1);
		calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
		Date date = calendar.getTime();
		DateFormat day = new SimpleDateFormat("dd");
		return Integer.parseInt(day.format(date));
	}
}
